package com.dm.cloud.images.controller.defalut;

import com.dm.cloud.core.mode.ResultModel;
import com.dm.cloud.images.mode.AppArrangeModel;
import com.dm.cloud.images.mode.AppContainerModel;
import com.dm.cloud.images.mode.AppInfoModel;
import com.dm.cloud.images.schedule.AppContainerTask;
import com.dm.cloud.oauthlib.service.anon.HasLogin;
import com.dm.cloud.tkmybatis.service.BaseEntityService;
import com.dm.cloud.tkmybatis.service.EntityBaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.dm.cloud.images.common.po.ImagesAppArrange;
import com.dm.cloud.images.common.dto.ImagesAppArrangeDTO;
import com.dm.cloud.images.common.request.ImagesAppArrangeSearchRequest;
import com.dm.cloud.images.service.ImagesAppArrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
   * @author hcx
   * @type ImagesAppArrangeController.java
   * @desc 容器编排信息表 ImagesAppArrange controller
   */
@RestController
@RequestMapping("/apis/v1/imagesAppArrange")
@Tag(name =  "容器编排信息表  ImagesAppArrangecontroller")
@HasLogin
public class ImagesAppArrangeController extends EntityBaseController<ImagesAppArrangeSearchRequest, ImagesAppArrange, ImagesAppArrangeDTO> {

    @Autowired
    private AppContainerTask appContainerTask;

    @Autowired
    private ImagesAppArrangeService imagesAppArrangeService;
    @Override
    public ImagesAppArrange getEntity() {
        return new ImagesAppArrange();
    }

    @Override
    public ImagesAppArrangeDTO getDTO() {
        return new ImagesAppArrangeDTO();
    }
    @Override
    public BaseEntityService<ImagesAppArrangeSearchRequest, ImagesAppArrange> getService() {
        return imagesAppArrangeService;
    }

    @GetMapping("appInfo")
    @Operation(summary = "应用基本信息统计")
    public ResultModel<AppInfoModel> appInfo(@Parameter(description = "appCode",in = ParameterIn.QUERY) String appCode){
        AppInfoModel appInfoModel=imagesAppArrangeService.appInfo(appCode);
        return ResultModel.success(appInfoModel);
    }

    @PostMapping("arrange")
    @Operation(summary = "执行编排操作，新增、修改")
    public ResultModel<AppArrangeModel> arrange(@RequestBody AppArrangeModel arrangeModel){
        AppArrangeModel re=imagesAppArrangeService.arrange(arrangeModel);
        return ResultModel.success(re);
    }

    @GetMapping("getArrange")
    @Operation(summary = "获取编排信息列表")
    public ResultModel<AppArrangeModel> getArrange(@Parameter(in = ParameterIn.QUERY,description = "应用code")String appCode){
        AppArrangeModel re=imagesAppArrangeService.getArrange(appCode);
        return ResultModel.success(re);
    }

    @GetMapping("startApp")
    @Operation(summary = "一键启动应用")
    public ResultModel<Boolean> startApp(@Parameter(in = ParameterIn.QUERY,description = "应用code")String appCode){
        Boolean isOk=imagesAppArrangeService.startApp(appCode);
        return ResultModel.success(isOk);
    }

    @GetMapping("stopApp")
    @Operation(summary = "一键关闭应用")
    public ResultModel<Boolean> stopApp(@Parameter(in = ParameterIn.QUERY,description = "应用code")String appCode){
        Boolean isOk=imagesAppArrangeService.stopApp(appCode);
        return ResultModel.success(isOk);
    }

    @GetMapping("restartApp")
    @Operation(summary = "一键重启应用")
    public ResultModel<Boolean> restartApp(@Parameter(in = ParameterIn.QUERY,description = "应用code")String appCode){
        Boolean isOk=imagesAppArrangeService.restartApp(appCode);
        return ResultModel.success(isOk);
    }

    @GetMapping("containerStatus")
    @Operation(summary = "应用容器的状态信息")
    public ResultModel<AppContainerModel> containerStatus(@Parameter(in = ParameterIn.QUERY,description = "应用code")String appCode){
        AppContainerModel containerModel=imagesAppArrangeService.containerStatus(appCode);
        return ResultModel.success(containerModel);
    }

    @GetMapping("tesstTask")
    @Operation(summary = "测试任务")
    public ResultModel<Boolean> testTask(){
        appContainerTask.taskRun();
        return ResultModel.success(true);
    }

}
